package com.yijiaw.util;

import java.io.File;
import java.util.Random;

import org.springframework.web.multipart.MultipartFile;

/**
 * spring处理文件上传辅助类
 * @author itachi
 *
 */
public class FileUploadUtil {
	
	/**
	 * spring上传文件
	 * @param file
	 * @param target
	 * @return
	 */
	public String write(MultipartFile file, File target) {
		try {
			file.transferTo(target);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return target.getName();
	}
	/**
	 * spring上传文件
	 * @param file
	 * @param path
	 * @return
	 */
	public String write(MultipartFile file, String path) {
		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));	//后缀
		String fileName = new Random(System.currentTimeMillis()).nextLong() + suffix;						//文件名，不带路径										
		return write(file, new File(path + "/" + fileName));
	}
	
}
